//------------------------------------------------------------------------
//                              LAnalysis
//------------------------------------------------------------------------
/** @file LAnalysis.java
 *
 * Autores: Guillermo Garcia Juanes
 *          Jose Hernandez Gonzalez
 * Octubre de 2011
 *
 * Procesadores de Lenguajes
 * Practica 2
 *
 * Descripcion:
 *  Clase encargada de gestionar los ficheros con la gramatica y las palabras
 *  reservadas, asi como el fichero.java al que se le realiza el analisis
 *  lexico.
 */
package pl.equipo13.p2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class LAnalysis {
	private FileReader Fset;
	private FileReader Fjava;
	private FileReader Fgr;
	private FileWriter Fout;
	private BufferedReader BufferIn;
	private PrintWriter BufferOut;
	private Grammar Gr;
	private Keywords Kw;
	private AAreas areas;
	private String Line;
	private int i;
	
	/** 
	 * Constructor
	*/
	public LAnalysis(){
		this.Gr = new Grammar();
		this.Kw = new Keywords();
		this.areas = new AAreas();
	}
	
	/**
	 * openFileG,
	 * Se encarga de abrir y leer un fichero
	 * donde se encuentra la gramatica del lenguaje
	 * 
	 * @param String namefile, nombre del fichero
	 */
	public void openFileGr(String namefile) {
		try {
			this.Fgr = new FileReader(new File(namefile));
			this.BufferIn = new BufferedReader(Fgr);
			
			this.Line = this.BufferIn.readLine();
	         
			this.Gr.ReadGrammar(this.Line);	         
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * openFileKw,
	 * Se encarga de abrir y leer un fichero
	 * donde se encuentran las palabras reservadas del lenguaje
	 * @param String namefilem nombre del fichero
	 */
	public void openFileKw(String namefile) {
		try {
			this.Fset = new FileReader(new File(namefile));
			this.BufferIn = new BufferedReader(Fset);
			while((this.Line = this.BufferIn.readLine()) != null){
				this.Kw.ReadKeywords(this.Line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Analysis
	 * @param namefile
	 */
	public void Analysis(String namefile) {
		try {
			this.Fout = new FileWriter(new File("Result.txt"));
			this.BufferOut = new PrintWriter(Fout);
			this.Fjava = new FileReader(new File(namefile));
			this.BufferIn = new BufferedReader(Fjava);
			this.i = 0;
			boolean comentario = false;
			
			this.BufferOut.println( "//---------- INICIO DEL INFORME\n\n");
			this.BufferOut.println( "//---------- INFORME GRAMATICA\n");
			
			while((this.Line = this.BufferIn.readLine()) != null) {
				this.i++;			

				if (!comentario) {
					int itext = this.Line.indexOf("\"");
					if (itext != -1)
						if (this.Line.indexOf("\"", itext + 1) != -1)
							this.Line = this.Line.substring(0, itext) + " " + this.Line.substring(this.Line.indexOf("\"", itext + 1) + 1);
						else {
							this.Line = this.Line.substring(0, itext);
						}
				
					int icom = this.Line.indexOf("/*");
					if (icom != -1)
						if (this.Line.indexOf("*/") != -1) {
							this.Line = this.Line.substring(0, icom) + " " + this.Line.substring(this.Line.indexOf("*/") + 2);
						} else {
							comentario = true;
							this.Line = this.Line.substring(0, icom);
						}
				} else {
					if (this.Line.indexOf("*/") != -1) {
						comentario = false;
						this.Line = this.Line.substring(this.Line.indexOf("*/") + 2);
					} else
						this.Line = " ";
				}
				
				StringTokenizer stText= new StringTokenizer(this.Line);
				while (stText.hasMoreTokens()) {
					String token = stText.nextToken();

					// Comprobacion palabras reservadas
					this.Kw.CheckKw(token);	
			
					//Comprobacion lexica linea a linea.
					if(!this.Gr.CheckGr(token)) {
						System.out.println(token);
						this.BufferOut.println("Error: Line " + i);
					}
			
					// Comprobacion de ambitos
					if(token.indexOf("{") != -1)
						this.areas.OpenArea();
					if(token.indexOf("}") != -1)
							this.areas.CloseArea();
					}
				}
			this.BufferOut.println(this.areas.Report());
			this.BufferOut.println(this.Kw.Report());
			this.BufferOut.println("//---------- FIN DEL INFORME");
			this.BufferOut.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * main
	 * @param argvs
	 */
	public static void main(String[] argvs){
		LAnalysis LA = new LAnalysis();
		
		//Lectura gramatica y palabras reservadas.
		LA.openFileGr(argvs[0]);
		LA.openFileKw(argvs[1]);
		
		//Lectura fichero .java
		LA.Analysis(argvs[2]);
	}
	
}
